package com.CTAtracker.android.api;

import android.util.Log;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

/**
 * This class is the only one to communicate with the CTA API 
 */
public class CTAResClient {
	
	/**
	 * The base URL of the API 
	 */
	private static final String BASE_URL = "http://lapi.transitchicago.com/api/1.0/ttarrivals.aspx?";
	/**
	 * The AsyncHttpClient that communicate with the API
	 */
	private static AsyncHttpClient _client = new AsyncHttpClient();
	/**
	 * Log
	 */
	private static final String TAG = "CTAResClient";
	
	/**
	 * Use the URI method "get" to send information to the API
	 * @param url the url to call  
	 * @param params the parameters provided with the url
	 * @param responseHandler the instance of the reponse
	 */
	public static void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
		Log.i(TAG, "Request sent by Get method");
		_client.get(getAbsoluteUrl(url), params, responseHandler);
	}

	/**
	 * Use the URI method "post" to send information to the API
	 * @param url the url to call
	 * @param params the parameters provided with the url 
	 * @param responseHandler the instance of the response
	 */
	public static void post(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
		Log.i(TAG, "Request sent by Post method");
		_client.post(getAbsoluteUrl(url), params, responseHandler);
		
	}

	/**
	 * Return the value of the url to call 
	 * @param relativeUrl the value to add to the BASE_URL
	 * @return the full URL of the API to call
	 */
	private static String getAbsoluteUrl(String relativeUrl) {
		return BASE_URL + relativeUrl;
	}

}
